package com.weitu.emergency.service.impl;

import com.weitu.emergency.entity.EmergencyTeam;
import com.weitu.emergency.entity.TeamAndRegion;
import com.weitu.emergency.entity.TeamAndUser;
import com.weitu.emergency.mapper.EmergencyTeamMapper;
import com.weitu.emergency.mapper.TeamAndRegionMapper;
import com.weitu.emergency.mapper.TeamAndUserMapper;
import com.weitu.emergency.service.EmergencyTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author zhangqingxi 2017-05-10
 */
@Service
public class EmergencyTeamServiceImpl implements EmergencyTeamService {

    private final EmergencyTeamMapper emergencyTeamMapper;
    private final TeamAndRegionMapper teamAndRegionMapper;
    private final TeamAndUserMapper teamAndUserMapper;



    @Autowired
    public EmergencyTeamServiceImpl(EmergencyTeamMapper emergencyTeamMapper, TeamAndRegionMapper teamAndRegionMapper, TeamAndUserMapper teamAndUserMapper) {
        this.emergencyTeamMapper = emergencyTeamMapper;
        this.teamAndRegionMapper = teamAndRegionMapper;
        this.teamAndUserMapper = teamAndUserMapper;
    }

    @Override
    public List<EmergencyTeam> findByParam(Map paramMap) {

        return emergencyTeamMapper.findByParam(paramMap);
    }

    @Override
    public Integer findByParamCount(Map paramMap) {
        return emergencyTeamMapper.findByParamCount(paramMap);
    }

    @Override
    public EmergencyTeam findById(String id) {
        return emergencyTeamMapper.findById(id);
    }

    @Transactional
    @Override
    public Integer add(Map paramMap, List<TeamAndUser> teamAndUserList,List<TeamAndRegion> teamAndRegionList) {

        //添加应急队伍 危险区域
        if(teamAndRegionList.size()>0){
            teamAndRegionMapper.add(teamAndRegionList);
        }
        //应急队伍人员
        if(teamAndUserList.size()>0){
            teamAndUserMapper.add(teamAndUserList);
        }


        return emergencyTeamMapper.add(paramMap);
    }

    @Transactional
    @Override
    public Integer updateById(Map paramMap) {
        return emergencyTeamMapper.updateById(paramMap);
    }

    @Override
    public Integer updateById(Map paramMap, List<TeamAndUser> teamAndUserList, List<TeamAndRegion> teamAndRegionList, String id) {

        teamAndRegionMapper.deleteByTeamId(id);
        teamAndUserMapper.deleteByTeamId(id);
        //添加应急队伍 危险区域
        if(teamAndRegionList.size()>0){
            teamAndRegionMapper.add(teamAndRegionList);
        }
        //应急队伍人员
        if(teamAndUserList.size()>0){
            teamAndUserMapper.add(teamAndUserList);
        }


        return emergencyTeamMapper.updateById(paramMap);
    }
}
